package Master.interfaces;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Master.Resource;
import Master.Task;
import Module.WorkService;
import Module.StatusService.JobStatus;

public class JobDetailDialog extends DetailDialog {
	private JLabel task_id = new JLabel("Task ID");
	private JLabel status = new JLabel("Status");
	private JLabel specific_worker = new JLabel("Worker");
	private JLabel time = new JLabel("Estimated time (sec)");
	private JLabel elapsed_time = new JLabel("Elapsed time (sec)");
	
	private JLabel task_id_data =new JLabel();
	private JLabel status_data = new JLabel();
	private JLabel specific_worker_data = new JLabel();
	private JLabel time_data = new JLabel();
	private JLabel elapsed_time_data = new JLabel();
	
	private JButton restart_btn = new JButton("Restart");
	private JButton cancel_btn = new JButton("Cancel");
	private JButton close_btn = new JButton("Close");
	
	private Task this_task;
	private JobStatus current_status;

	public JobDetailDialog(String title,Task this_task,JobStatus status) {
		super(title);
		this.this_task=this_task;
		this.setSize(300,160);
		
		initialiseGUI();
		initialiseListeners();
		loadData(status);
	}
	
	private void initialiseGUI()
	{
		this.setLayout(new BorderLayout());
		JPanel button_p=new JPanel();
		button_p.setLayout(new FlowLayout());
		this.add(button_p,BorderLayout.SOUTH);
		
		button_p.add(close_btn);
		button_p.add(cancel_btn);
		button_p.add(restart_btn);
		
		JPanel info_p=new JPanel();
		info_p.setLayout(new GridBagLayout());
		this.add(info_p,BorderLayout.WEST);
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		
		c.insets=new Insets(1,5,1,5);
		c.gridx = 0;
		c.gridy = 0;
		info_p.add(task_id,c);
		
		c.gridx = 0;
		c.gridy = 1;
		info_p.add(specific_worker,c);
		
		c.gridx = 0;
		c.gridy = 2;
		info_p.add(status,c);
		
		c.gridx = 0;
		c.gridy = 3;
		info_p.add(time,c);
		
		c.gridx = 0;
		c.gridy = 4;
		info_p.add(elapsed_time,c);
		
		c.gridx = 1;
		c.gridy = 0;
		info_p.add(task_id_data,c);
		
		c.gridx = 1;
		c.gridy = 1;
		info_p.add(specific_worker_data,c);
		
		c.gridx = 1;
		c.gridy = 2;
		info_p.add(status_data,c);
		
		c.gridx = 1;
		c.gridy = 3;
		info_p.add(time_data,c);
		
		c.gridx = 1;
		c.gridy = 4;
		info_p.add(elapsed_time_data,c);
	}
	
	private void loadData(JobStatus status)
	{
		current_status=status;
		task_id_data.setText(""+this_task.getTaskID());
		time_data.setText(""+this_task.getEstimateTime());
		specific_worker_data.setText(""+this_task.getSpecifiedResource().getID());
		elapsed_time_data.setText(this_task.getElapsedTime()==0?"0":this_task.getElapsedTime()+"");
		
		switch(status)
		{
			case done:
				status_data.setText("Completed");
				cancel_btn.setEnabled(false);
				restart_btn.setEnabled(false);
				break;
			case failed:
				status_data.setText("Failed");
				cancel_btn.setEnabled(false);
				restart_btn.setEnabled(true);
				break;
			case running:
				status_data.setText("Executing on Worker "+this_task.getSpecifiedResource().getID());
				cancel_btn.setEnabled(true);
				restart_btn.setEnabled(false);
				break;
			case parepare:
				status_data.setText("Converting to executable");
				cancel_btn.setEnabled(false);
				restart_btn.setEnabled(false);
				break;
			case ready:
				status_data.setText("Waiting to be executed");
				cancel_btn.setEnabled(false);
				restart_btn.setEnabled(false);
				break;
			case cancel:
				status_data.setText("Cancelled by user");
				cancel_btn.setEnabled(false);
				restart_btn.setEnabled(true);
				break;
		}
	}
	
	private void initialiseListeners()
	{
		close_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		
		cancel_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Resource resource = this_task.getSpecifiedResource();
		        WorkService workService = resource.getWorkService();

		        try {
		            workService.cancelJob(resource.getAuthCode());
		            loadData(JobStatus.cancel);
		        }catch (Exception ex){
		            ex.printStackTrace();
		        }
			}
		});
		
		restart_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(current_status==JobStatus.failed || current_status==JobStatus.cancel)
				{
					loadData(JobStatus.ready);
					this_task.getSpecifiedResource().restartTask(this_task);
					try {
						this_task.getSpecifiedResource().getStatusService().setJobStatus(this_task.getTaskID(),
								JobStatus.ready, this_task.getSpecifiedResource().getAuthCode());
					} catch (RemoteException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
	}
	
	private void close()
	{
		this.dispose();
	}
}
